#!/usr/bin/env python

import os
import inkex

class Stylist(inkex.Effect):
    """Remove style attributes when an attribute class exist"""
    def __init__(self):
        """ main stream"""
        inkex.Effect.__init__(self)
        self.OptionParser.add_option("-b", "--book",
                        action="store", type="string",
                        dest="book", default="",
                        help="Style type")        
        self.OptionParser.add_option("-o", "--object",
                        action="store", type="string",
                        dest="object", default="",
                        help="Object to insert")
                        
    def effect(self) :
        """add a use symbol"""
        for id, node in self.selected.iteritems():
            new = inkex.etree.SubElement(node.getparent(),inkex.addNS('use','svg'))
            new.set(inkex.addNS('href','xlink'), "#"+self.options.object)
            if self.options.ids :
                id = self.options.ids[0]
                #query inkscape about the bounding box of obj
                #TODO Find a better way to get these values. Too slow
                self.q = {'x':0,'y':0,'width':0,'height':0}
                file = self.args[-1]
                for query in self.q.keys():
                    _,f,err = os.popen3("inkscape --query-%s --query-id=%s %s" % (query,id,file))
                    self.q[query] = float(f.read())
                    f.close()
                    err.close()
                    x = self.q['x']+self.q['width']/2
                    y = self.q['y']+self.q['height']/2
            else : 
                x = self.view_center[0]
                y = self.view_center[1]
            new.set('x',str(x))
            new.set('y',str(y))
        
# Create effect instance and apply it.
Effect = Stylist()
Effect.affect()